package com.buss.core.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dao.OrganizationDao;
import com.buss.core.model.Organization;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.PersistenceCond;
import com.buss.core.persistence.ResultList;

@Repository
public class OrganizationDaoImpl implements OrganizationDao {

	private static final long serialVersionUID = -3662224580021203957L;

	@Autowired
	private BaseDao<Organization> baseDao;
	
	public void deleteOrganization(Long[] ids) {
		String hql = "delete from Organization organ where organ.id in (:ids)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ids", ids);
		//baseDao.remove(id, Organization.class);
		baseDao.excuteHsql(hql, map);
	}
	
	
	
	public void editOrganization(Organization adminUserOrgan) {
		baseDao.edit(adminUserOrgan);

	}

	public Organization loadOrganization(Long id) {
		return baseDao.get(id, Organization.class);//.load(id, Organization.class);
	}

	public ResultList<Organization> queryByParentId(String companyId,Long parentId,OrderBy orderBy) {
		String sql = "select auo from Organization auo where " +
				" auo.companyId = :companyId and auo.parentId = :parentId";
		PersistenceCond cond = new PersistenceCond();
		cond.add("companyId", companyId);
		cond.add("parentId", parentId);
		if(orderBy != null){
			sql += orderBy.aggOrder("auo");
		}
		return baseDao.queryHsql(sql, cond, null);
	}

	public void saveOrganization(Organization organization) {
		baseDao.save(organization);
	}
	
	public Organization mergeOrganization(Organization organization){
		return baseDao.saveOrEdit(organization);
	}



	public void updateNodeNum(Long id,long num) {
		if(num != 0){
			String sql = "update Organization auo set auo.nodeNum = auo.nodeNum - :num where auo.id = :id";
			PersistenceCond cond = new PersistenceCond();
			cond.add("id", id);
			cond.add("num", num);
			baseDao.excuteHsql(sql, cond);
		}
		
	}

}
